package net.qhzw.vip.qiyu.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import net.qhzw.vip.qiyu.entity.PickAddr;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.PickAddrService;
import net.qhzw.vip.qiyu.util.PageParam;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;

@RestController
@RequestMapping("/shop/pickAddr")
public class PickAddrController {

    @Autowired
    private PickAddrService pickAddrService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    public JsonResult<IPage<PickAddr>> page(PickAddr pickAddr, PageParam<PickAddr> page){
        IPage<PickAddr> pickAddrs = pickAddrService.page(page,new LambdaQueryWrapper<PickAddr>()
                .like(StrUtil.isNotBlank(pickAddr.getAddrName()),PickAddr::getAddrName,pickAddr.getAddrName())
                .orderByDesc(PickAddr::getAddrId));
        return JsonResult.ok(pickAddrs);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
    public JsonResult<PickAddr> info(@PathVariable("id") Long id){
        PickAddr pickAddr = pickAddrService.getById(id);
        return JsonResult.ok(pickAddr);
    }

    /**
     * 保存
     */
    @PostMapping
    public JsonResult save(@Valid @RequestBody PickAddr pickAddr){
        pickAddr.setShopId(SecurityUtils.getLoginUser().getShopId());
        pickAddrService.save(pickAddr);
        return JsonResult.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    public JsonResult update(@Valid @RequestBody PickAddr pickAddr){
        PickAddr dbPickAddr = pickAddrService.getById(pickAddr.getAddrId());
        pickAddrService.updateById(pickAddr);
        return JsonResult.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping
    public JsonResult delete(@RequestBody Long[] ids){
        pickAddrService.removeByIds(Arrays.asList(ids));
        return JsonResult.ok();
    }
}

